/**
 * 微信小程序购物车模块控制层
 *
 * @author JINMO
 * @since 2022/06/28 19:26
 */

package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketCart;
import com.cskaoyan.bean.bo.MarketCartCheckedBo;
import com.cskaoyan.bean.bo.MarketCartDeleteBo;
import com.cskaoyan.bean.vo.cart.MarketCartCheckOutVo;
import com.cskaoyan.bean.vo.cart.MarketCartDeleteVo;
import com.cskaoyan.service.admin.MarketCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("wx/cart")
public class WXCartController {

    @Autowired
    MarketCartService marketCartService;

    /**
     * 获取购物车的数据
     *
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/28 19:41:07
     */
    @RequestMapping("index")
    public BaseRespVo index() {
        MarketCartDeleteVo data = marketCartService.index();

        return BaseRespVo.ok(data);
    }

    /**
     * 添加商品到购物车,返回购物车的商品个数
     *
     * @param marketCart 购物车
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/28 20:30:58
     */
    @RequestMapping("add")
    public BaseRespVo add(@RequestBody MarketCart marketCart) {
        //添加商品到购物车
        marketCartService.add(marketCart);
        //购物车的商品个数
        Integer data = marketCartService.goodsCount();

        return BaseRespVo.ok(data);
    }

    /**
     * 获取购物车商品件数
     *
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/28 21:40:16
     */
    @RequestMapping("goodscount")
    public BaseRespVo goodscount() {
        Integer data = marketCartService.goodsCount();

        return BaseRespVo.ok(data);
    }

    /**
     * 选择或取消选择商品,返回购物车信息
     *
     * @param marketCartCheckedBo 市场检查车博
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/28 22:04:54
     */
    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody MarketCartCheckedBo marketCartCheckedBo) {
        marketCartService.checked(marketCartCheckedBo);

        return BaseRespVo.ok(marketCartService.index());
    }

    /**
     * 更新购物车 商品数量信息
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Can
     * @since 2022/06/29 16:05
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Map map) {
        BaseRespVo baseRespVo = marketCartService.update(map);
        return baseRespVo;
    }

    /**
     * 下单前信息确认
     *
     * @param marketCartCheckOutVo 市场购物车查看签证官
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/29 14:03:50
     */
    @RequestMapping("checkout")
    public BaseRespVo checkout(MarketCartCheckOutVo marketCartCheckOutVo) {
        MarketCartCheckOutVo data = marketCartService.checkout(marketCartCheckOutVo);

        return BaseRespVo.ok(data);
    }

    /**
     * 立即购买商品
     *
     * @param marketCart 市场车
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/29 15:29:13
     */
    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody MarketCart marketCart) {
        //添加商品到购物车,返回购物车id
        Integer data = marketCartService.fastadd(marketCart);
        //返回商品购物车id
        return BaseRespVo.ok(data);
    }


    /**
     * 删除购物车的商品
     *
     * @param marketCartDeleteBo
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/29 18:13:54
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCartDeleteBo marketCartDeleteBo) {
        // Integer[] integers = new ObjectMapper().readValue(productIds, Integer[].class);
        //Integer[] 转换为list
        // List<Integer> list = Arrays.asList(productIds);
        MarketCartDeleteVo data = marketCartService.delete(marketCartDeleteBo.getProductIds());
        //删除操作
        // marketCartService.delete(marketCartDeleteBo.getProductIds());
        return BaseRespVo.ok(data);
    }




}
